package com.rzico.sbl.ui.report.manager

import android.widget.TextView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityReportBarrelBrandBinding
import com.rzico.sbl.model.ManagerOrderData
import com.rzico.sbl.other.getBeforeDate
import com.rzico.sbl.other.getEndDate
import com.rzico.sbl.other.getFirstDate
import com.rzico.sbl.other.getTodayDate
import com.rzico.sbl.ui.report.other.goToOrderDetail
import com.rzico.sbl.viewmodel.ReportBarrelViewModel
import com.xinnuo.common.extend.get
import com.xinnuo.common.extend.getExtra
import com.xinnuo.common.extend.gone
import com.xinnuo.common.extend.oneClick
import com.xinnuo.common.helper.TimeHelper
import com.xinnuo.common.helper.getTimeString
import com.xinnuo.common.listener.onTabSelectedListener
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.count
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.showTimeDialog
import com.xinnuo.slimadapter.SlimAdapter

class ReportBarrelBrandActivity : BaseActivity(R.layout.activity_report_barrel_brand) {

    private val mBinding by lazy { ActivityReportBarrelBrandBinding.bind(getRootView()) }

    private lateinit var mDateHint: String
    private lateinit var mBeginDate: String
    private lateinit var mEndDate: String
    private var mOrderType = ""

    override fun getViewModel() = get<ReportBarrelViewModel>()

    override fun initData() {
        initTitle(title = "出入库", name = mDateHint)

        initLayout()
        initTabLayout()
    }

    private fun initLayout() {
        mBinding.brandBottom.gone()

        mBinding.listLayout.apply {
            emptyLayout.also {
                it.emptyHint.text = "暂无相关出入库信息！"
            }

            recycleList.also { view ->
                view.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        if (!isLoadingMore) {
                            isLoadingMore = true
                            getData(pageNum)
                        }
                    }
                )

                view.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                mAdapter = SlimAdapter.creator()
                    .register<ManagerOrderData>(R.layout.item_report_barrel) { jector, bean, _ ->
                        jector.apply {
                            gone(R.id.item_barrel_type)
                            text(R.id.item_barrel_title, bean.name)
                            text(R.id.item_barrel_sn, bean.sn)
                            text(R.id.item_barrel_time, bean.createDate.getTimeString())
                            text(R.id.item_barrel_num, bean.quantity)
                            text(R.id.item_barrel_name, bean.ename)
                            text(R.id.item_barrel_shop, bean.aname)

                            clicked {
                                goToOrderDetail("orderSn" to bean.sn)
                            }
                        }
                    }
                    .attachTo(view)
            }
        }

        tvRight?.oneClick { text ->
            text as TextView

            getViewModel()
                .showDropTimeFilter(
                    context = getIContext(),
                    bottom = text,
                    list = getViewModel().mTimeList
                ) { bean ->

                    if (bean.id != "3") {
                        getViewModel().mTimeList.last().name = "自定义月份"
                    }

                    when (bean.id) {
                        "1" -> {
                            mBeginDate = getBeforeDate(6)
                            mEndDate = getTodayDate()
                            text.text = "近7天"

                            updateList()
                        }
                        "2" -> {
                            mBeginDate = getFirstDate()
                            mEndDate = getEndDate()
                            text.text = "本月"

                            updateList()
                        }
                        "3" -> {
                            showTimeDialog(
                                minYearValue = 2021,
                                count = 2,
                                title = "请选择月份",
                                isCurrentDate = true,
                                isAfterLimited = true,
                                callback = { year, month, _, _, _, hint ->
                                    getViewModel()
                                        .mTimeList
                                        .filter { it.isChecked }
                                        .forEach { it.isChecked = false }
                                    getViewModel()
                                        .mTimeList
                                        .last()
                                        .isChecked = true

                                    mBeginDate = "$hint-01"
                                    mEndDate = "$hint-${TimeHelper.getInstance().getLastDayOfMonth(year, month)}"
                                    text.text = hint
                                    bean.name = hint

                                    updateList()
                                }
                            )
                        }
                    }
                }
        }
    }

    override fun initListener() {
        mDateHint = intent.getExtra("dateHint")
        mBeginDate = intent.getExtra("beginDate")
        mEndDate = intent.getExtra("endDate")

        when (mDateHint) {
            "近7天" -> getViewModel().mTimeList[0].isChecked = true
            "本月" -> getViewModel().mTimeList[1].isChecked = true
            else -> {
                getViewModel().mTimeList[2].isChecked = true
                getViewModel().mTimeList[2].name = mDateHint
            }
        }
    }

    /**
     * TabLayout 初始化
     */
    private fun initTabLayout() {
        mBinding
            .brandTab
            .apply {
                onTabSelectedListener {
                    onTabSelected {
                        it?.let {
                            mOrderType = when (it.position) {
                                0 -> "quantity"
                                1 -> "returnQuantity"
                                else -> ""
                            }

                            getViewModel().cancelRequest()
                            getViewModel().delay { updateList() }
                        }
                    }
                }

                addTab(this.newTab().setText("出库"), true)
                addTab(this.newTab().setText("入库"), false)
            }
    }

    /**
     * 出入库空桶列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .barrelList(
                page = index,
                type = mOrderType,
                beginDate = mBeginDate,
                endDate = mEndDate,
                onSuccess = {
                    mList.apply {
                        if (index == 0) {
                            clear()
                            pageNum = 0
                        }
                        addItems(it)
                        if (it.count() > 0) pageNum++
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

    /**
     * 刷新列表数据
     */
    private fun updateList() {
        mBinding
            .listLayout
            .apply {
                swipeRefresh.isRefreshing = true

                emptyLayout
                    .emptyView
                    .gone()
            }

        if (mList.isNotEmpty()) {
            mList.clear()
            mAdapter.notifyDataSetChanged()
        }

        getData(0)
    }

}